package cn.edu.nuc.ssm.service.impl;

import cn.edu.nuc.ssm.dao.BbsReplyMapper;
import cn.edu.nuc.ssm.dao.BbsSectionMapper;
import cn.edu.nuc.ssm.dao.BbsTopicMapper;
import cn.edu.nuc.ssm.entity.BbsReply;
import cn.edu.nuc.ssm.entity.BbsSection;
import cn.edu.nuc.ssm.entity.BbsTopic;
import cn.edu.nuc.ssm.service.BbsSectionService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2017/4/20.
 */
@Service
public class BbsSectionServiceImpl implements BbsSectionService{

    @Autowired
    private BbsSectionMapper bbsSectionMapper;

    @Autowired
    private BbsTopicMapper bbsTopicMapper;

    @Autowired
    private BbsReplyMapper bbsReplyMapper;

    @Override
    public List<String> findSectionType() {
        return bbsSectionMapper.findSectionType();
    }

    @Override
    public List<BbsSection> findbySectionType(String sectionType) {

        return bbsSectionMapper.findbySectionType(sectionType);
    }

    @Override
    public List<String> findAllSectionType() {

        return bbsSectionMapper.findAllSectionType();
    }

    @Override
    public List<BbsSection> find() {
        return bbsSectionMapper.find();
    }

    @Override
    public BbsSection findbyId(Integer id) {
        return bbsSectionMapper.findbyId(id);
    }

    @Override
    public void count(int id) {

        int num = bbsTopicMapper.countbySid(id);

         bbsSectionMapper.updatetopicCountBYId(num, id);


    }

    @Override
    public void updateSelective(BbsSection bbsSection) {

         bbsSectionMapper.updateByPrimaryKeySelective(bbsSection);
    }

    @Override
    public List<BbsSection> findbySname(@Param("sname") String sname) {

        return bbsSectionMapper.findbySname(sname);
    }

    @Override
    public int add(BbsSection bbsSection) {

        return bbsSectionMapper.insertSelective(bbsSection);
    }

    @Override
    public int delete(Integer id) {

        List<BbsReply> bbsReplyList = bbsReplyMapper.findbySid(id);

        if (bbsReplyList.size()>0){

            bbsReplyMapper.deletebySid(id);
        }

        List<BbsTopic> bbsTopics = bbsTopicMapper.findbySid(id);
        if (bbsTopics.size()>0){

            bbsTopicMapper.deletebySid(id);
        }

        bbsSectionMapper.deleteByPrimaryKey(id);

        return 1;
    }
}
